package cn.zhaozhiqi.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.zhaozhiqi.constants.Constants;
import cn.zhaozhiqi.constants.ErrorCode;
import cn.zhaozhiqi.pojo.domain.Customer;
import cn.zhaozhiqi.mapper.CustomerMapper;
import cn.zhaozhiqi.pojo.dto.AddCarDto;
import cn.zhaozhiqi.pojo.dto.CustomerRegisterDto;
import cn.zhaozhiqi.pojo.vo.WXRegisterVo;
import cn.zhaozhiqi.remote.api.ILoginFeignClient;
import cn.zhaozhiqi.remote.dto.LoginDto;
import cn.zhaozhiqi.service.ICustomerService;
import cn.zhaozhiqi.service.ICustomerSummaryService;
import cn.zhaozhiqi.service.ICustomerWalletService;
import cn.zhaozhiqi.template.WeChatTemplate;
import cn.zhaozhiqi.utils.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author zhaozhiqi
 * @since 2024-12-21
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {


    @Autowired
    private ICustomerSummaryService summaryService;
    @Autowired
    private ICustomerWalletService walletService;
    @Autowired
    private WeChatTemplate weChatTemplate;
    @Autowired
    private ILoginFeignClient feignClient;
    @Override
    @Transactional
    public Boolean customerRegister(CustomerRegisterDto dto) {
        //封装了一个start的公共依赖
        WXRegisterVo wxRegisterVo = weChatTemplate.getOpenid(dto.getCode());
        //拿到openid我们需要先判断是否数据库中有这个数据，这就需要重新写一个专属mapper。这样我们可以用mybatisplus中的工具类生成
        LambdaQueryWrapper<Customer> wapper = new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, wxRegisterVo.getOpenid());
        //通过ServiceImpl内置的方法为我们寻找是否有满足要求的Customer
        Customer customer = super.getOne(wapper);
        //判断拿到的customer是否为空，如果不为空的话就直接抛出异常,使用hutool工具包
        AssertUtil.isNull(customer, ErrorCode.PARAM_CUSTOMER_EXIST);
        /*如果为空，就进行新增
        我们需要观察哪些表需要同时进行新增？一般情况下是一对一关系的表均需要进行新增操作
         */
        //根据hutool内的id工具类的雪花算法生成id
        Snowflake snowflakeID = IdUtil.createSnowflake(2, 4);
        long CustomerId = snowflakeID.nextId();
        //保存乘客
        createCustomer(wxRegisterVo,CustomerId);
        //保存乘客注册的关联表
        summaryService.create(CustomerId);
        walletService.create(CustomerId);

        //保存乘客的登录表
        LoginDto loginDto = new LoginDto();
        loginDto.setId(CustomerId);
        loginDto.setType(Constants.Driver.TYPE_CUSTOMER);
        loginDto.setOpenId(wxRegisterVo.getOpenid());
        feignClient.register(loginDto);
        return true;
    }



    //保存乘客的方法
    private void createCustomer(WXRegisterVo wxRegisterVo, long CustomerId) {
        Customer customer;
        //因为是空，所以就拿出来操作
        customer = new Customer();
        //进行乘客表的保存
        customer.setId(CustomerId);
        customer.setCreateTime(new Date());
        customer.setOpenId(wxRegisterVo.getOpenid());
        super.save(customer);
    }
}
